<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录 - 名片维护系统</title>
    <link rel="stylesheet" th:href="@{/css/login.css}">
</head>
<body>
    <div class="login-page">
        <div class="login-container">
            <h2 class="login-title">用户登录</h2>
            <div class="login-message error" id="error-message" style="display: none;"></div>

            <form id="login-form">
                <div class="login-form-group">
                    <label class="login-label" for="username">用户名</label>
                    <input class="login-input" type="text" id="username" name="username" required>
                </div>

                <div class="login-form-group">
                    <label class="login-label" for="password">密码</label>
                    <input class="login-input" type="password" id="password" name="password" required>
                </div>

                <div class="login-form-group">
                    <label class="login-label" for="captcha">验证码</label>
                    <div class="captcha-container">
                        <input class="login-input captcha-input" type="text" id="captcha" name="captcha" required>
                        <img class="captcha-image" id="captcha-image" src="/user/captcha" alt="验证码" onclick="refreshCaptcha()">
                    </div>
                </div>

                <button class="login-button" type="submit">登 录</button>
            </form>

            <div class="login-links">
                <a class="login-link" href="/user/register">注册账号</a>
                <a class="login-link" href="/user/forget-password">忘记密码</a>
                <a class="login-link" href="/admin/login">管理员登录</a>
            </div>
        </div>
    </div>

    <script>
        // 刷新验证码
        function refreshCaptcha() {
            document.getElementById('captcha-image').src = '/user/captcha?' + new Date().getTime();
        }

        // 表单提交
        document.getElementById('login-form').addEventListener('submit', function(e) {
            e.preventDefault();

            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const captcha = document.getElementById('captcha').value;

            // 表单验证
            if (!username || !password || !captcha) {
                showError('请填写完整信息');
                return;
            }

            // 发送登录请求
            fetch('/user/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Accept': 'application/json'  // 明确要求返回JSON
                },
                body: JSON.stringify({
                    username: username,
                    password: password,
                    captcha: captcha
                })
            })
            .then(response => {
                // 检查响应内容类型
                const contentType = response.headers.get('content-type');
                if (!contentType || !contentType.includes('application/json')) {
                    return response.text().then(text => {
                        console.error('Non-JSON response:', text);
                        throw new Error('服务器返回了非JSON格式的响应，请联系管理员');
                    });
                }
                return response.json();
            })
            .then(data => {
                if (data.success) {
                    window.location.href = '/'; // 登录成功，跳转到首页
                } else {
                    showError(data.message);
                    refreshCaptcha();
                }
            })
            .catch(error => {
                console.error('Error:', error);
                showError(error.message || '服务器错误，请稍后再试');
                refreshCaptcha();
            });
        });

        // 显示错误信息
        function showError(message) {
            const errorElement = document.getElementById('error-message');
            errorElement.textContent = message;
            errorElement.style.display = 'block';
        }
    </script>
</body>
</html>


